﻿using Riba.MVVMSL;

namespace MVVMtutorial
{
	public class Dialog1ViewModel : ViewModelBase
	{
		public RelayCommand CommandLoadChild { get; private set; }
		public RelayCommand CommandResetIsDirty { get; private set; }

		public Dialog1ViewModel()
		{
			CommandLoadChild = new RelayCommand(OnLoadChild) { IsEnabled = true };
			
			CommandResetIsDirty = new RelayCommand
				(
					cmdPara =>
						{
							PropertyChanged -= Dialog1ViewModel_PropertyChanged;
							IsDirty = false;
							PropertyChanged += Dialog1ViewModel_PropertyChanged;
						}
				){IsEnabled = true};

			PropertyChanged += Dialog1ViewModel_PropertyChanged;
		}

		void Dialog1ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			IsDirty = true;
		}

		private void OnLoadChild(object commandParameter)
		{
			var args = new LoadChildWindowEventArgs(
				typeof(ChildWindow1), ExchangeData, OnLoadViewCallback);

			RaiseLoadChildWindow(args);
		}

		public void OnLoadViewCallback(ViewModelBase vm, object exchangeData)
		{
			ExchangeData = exchangeData;
		}
	}
}
